<?php
	/**
	 *	Wap Portal Server
	 *
	 *	Copyright (C) 2002 - 2004 Emir Sakic
	 *
	 *	Wap Portal Server
	 *	Developer: Emir Sakic - saka@hotmail.com
	 *  Homepage: http://www.sakic.net
	 *	Date: 15/03/2004
	 * 	Version #: 1.2
	**/

// Add to database
if (isset($poll_id)) {
	if (isset($published) && $published == 'Yes') {
		$published = 1;
	} else {
		$published = 0;
	}

	// Replace quotes
	if (!get_magic_quotes_gpc()) {
		$polltitle = str_replace("'","\'",$polltitle);
	}

	if ($poll_id == 'New') {
		$query = "INSERT INTO wps_poll VALUES ( '', '$polltitle', '$polldate', '$published')";
	} else {
		$query = "UPDATE wps_poll SET question = '$polltitle', published = '$published' WHERE id = '$poll_id'";
	}
		$result = mysql_query($query) or die("Query: ".$query." failed");
	$poll_id = 0;
}

// Delete from database
if (isset($del_id)) {
	$query = "DELETE FROM wps_poll WHERE id = '$del_id'";
	mysql_query($query) or die("Delete failed");
	$query = "DELETE FROM wps_poll_data WHERE pollid = '$del_id'";
	mysql_query($query) or die("Delete failed");
	$del_id = 0;
}

// Update questions
if (isset($data_alt) && $data_alt!="") {
	for ($s = 0; $s < count($data_alt); $s++){
		//if (isset($data_id[$s])) {
			$query = "SELECT id FROM wps_poll_data WHERE id = '$data_id[$s]'";
			$result = mysql_query($query);
			$num = mysql_num_rows($result);
			if ($num!=0 && $data_alt[$s]=="") {
				$query = "DELETE FROM wps_poll_data WHERE id = '$data_id[$s]'";
			} else if ($num!=0) {
				$query = "UPDATE wps_poll_data SET alternative = '$data_alt[$s]', count = '$data_count[$s]' WHERE id = '$data_id[$s]'";
			} else if ($num==0 && $data_alt[$s]!="") {
				$query = "INSERT INTO wps_poll_data VALUES ( '', '$data_alt[$s]', $pollid, 0)";
			}
			//echo $query."<br>";
			//echo $num."<br>";
			mysql_query($query) or die("Query: ".$query." failed");
		//}
	}
}
?>
<script language="JavaScript">
<!--
function validate(){
	if ((document.form.polltitle.value=="")){
		alert('Please complete the form!');
		return false;
		}
	else {
		return true;
		}
	}
//-->
</script>

  <table width="600">
    <tr>

    <td colspan="3"><a href='index.php' border=0>Home</a> &gt; Poll Management</td>
    </tr>
    <tr>

    <td align="center" width="25%"><b>Manage&nbsp;Polls</b></td>

    <td align="center" width="50%"><b>Add/Edit Polls</b></td>
	  <td align="center" width="25%"><b>Resources</b></td>
    </tr>
    <tr>
      <td valign="top">
        <?// List existing poll titles
print "<a href=\"index.php?option=poll\">Add Poll</a><br><br>\n";
$query = "SELECT * FROM wps_poll ORDER BY date DESC";
$result = mysql_query($query);
for ($i = 0; $row = mysql_fetch_row ($result); $i++) {
	print "        <a href=\"index.php?option=poll&pollid=$row[0]\">$row[1]</a><br>\n";
}?>
      </td>

    <td valign="top">
      <?// Read poll from mySQL database
if (isset($pollid)) {
	$query = "SELECT * FROM wps_poll WHERE id = '$pollid'";
	$result = mysql_query($query);
	$row = mysql_fetch_row($result);
}
?>
      <form name="form" method="post" action="index.php?<? echo $_SERVER['QUERY_STRING']; ?>" onSubmit="return validate()">
          <p>
            <input type="text" name="polltitle" value="<? if (isset($pollid)) { echo $row[1]; } ?>">
            <br>
            <?php if (isset($pollid)) {
echo $row[2];
} else {
$query = "SELECT * FROM wps_timediff";
$result = mysql_query($query);
$row = mysql_fetch_row ($result);
$timeadjust = ($row[1] * 60 * 60);
$localtime = date("Y-m-d", time() + $timeadjust);
echo $localtime;
print "<input type=\"hidden\" name=\"polldate\" value=\"$localtime\">";
}?>
          </p>
          <p>Published: <input type="checkbox" name="published" value="Yes"<? if (empty($pollid) || $row[3]==1) echo " checked"; ?>></p>
          <input type="hidden" name="poll_id" value="<? if (isset($pollid)) { echo $row[0]; } else { echo 'New'; } ?>">
          <input type="submit" name="Submit" value="Submit">
        </form>
        <? if (isset($pollid)) {
		print "<form name=\"delete\" method=\"post\" action=\"index.php?option=poll\">
		<input type=\"hidden\" name=\"del_id\" value=\"$row[0]\">
		<input type=\"submit\" name=\"delete\" value=\"Delete\">
		</form>\n";

		$query = "SELECT * FROM wps_poll_data WHERE pollid = '$pollid' ORDER BY id";
		$result = mysql_query($query);
		echo "<hr><b>Alternatives:</b>\n";
		echo "<form name=\"data\" method=\"post\" action=\"index.php?option=poll\">\n";
		for ($i = 1; $row = mysql_fetch_row ($result); $i++) {
			echo "<input type=\"hidden\" name=\"data_id[]\" value=\"$row[0]\">\n";
			echo "$i <input type=\"text\" name=\"data_alt[]\" value=\"$row[1]\">\n";
			echo "<input type=\"text\" name=\"data_count[]\" value=\"$row[3]\" size=\"3\">\n";
			echo "<br>\n";
		}
		echo "$i <input type=\"text\" name=\"data_alt[]\"><br><br>\n";
		echo "<input type=\"hidden\" name=\"pollid\" value=\"$pollid\">\n";
		echo "<input type=\"submit\" name=\"update\" value=\"Submit\">\n";
		echo "</form>\n";
		}?>
      </td>
    <td valign="top"><b>Add</b> a Poll first.<br>
      <b>Edit</b> a Poll to add alternatives.<br>
      <b>Clear</b> an alternative to remove it.<br>
      <br>
      <br>
<?php
	$common->setup_default_emulator();
	$common->display_emulator();
?>
    </td>
    </tr>
  </table>
